﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ABELGX
{
    public class InfoWarning : CarInfo
    {
        public List<WarningGroup> Groups { get; set; } = new List<WarningGroup>();
        public DateTime WarningTime { get; set; }
        public InfoWarning(params byte[] func) : base(func)
        {
            Groups.Add(new WarningGroup("角度传感器")
            {
                Items = new List<WarningItem>() 
                {
                    new WarningItem("1号角度传感器报警", 1, 0, "正常", "报警"),
                    new WarningItem("2号角度传感器报警", 1, 1, "正常", "报警"),
                    new WarningItem("3号角度传感器报警", 1, 2, "正常", "报警"),
                    new WarningItem("4号角度传感器报警", 1, 3, "正常", "报警"),
                    new WarningItem("5号角度传感器报警", 1, 4, "正常", "报警"),
                    new WarningItem("6号角度传感器报警", 1, 5, "正常", "报警"),
                    new WarningItem("底盘倾角异常", 1, 6, "正常", "报警"),
                    new WarningItem("机械臂倾角异常", 1, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("温湿度传感器")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("1号温度传感器报警", 2, 0, "正常", "报警"),
                    new WarningItem("2号温度传感器报警", 2, 1, "正常", "报警"),
                    new WarningItem("3号温度传感器报警", 2, 2, "正常", "报警"),
                    new WarningItem("4号温度传感器报警", 2, 3, "正常", "报警"),
                    new WarningItem("湿度传感器报警", 2, 4, "正常", "报警"),
                    new WarningItem("高温报警", 2, 5, "正常", "报警"),
                    new WarningItem("低温报警", 2, 6, "正常", "报警"),
                    new WarningItem("湿度过高报警", 2, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("避障传感器")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("1号避障传感器故障", 3, 0, "正常", "报警"),
                    new WarningItem("2号避障传感器故障", 3, 1, "正常", "报警"),
                    new WarningItem("3号避障传感器故障", 3, 2, "正常", "报警"),
                    new WarningItem("4号避障传感器故障", 3, 3, "正常", "报警"),
                    new WarningItem("5号避障传感器故障", 3, 4, "正常", "报警"),
                    new WarningItem("6号避障传感器故障", 3, 5, "正常", "报警"),
                    new WarningItem("7号避障传感器故障", 3, 6, "正常", "报警"),
                    new WarningItem("8号避障传感器故障", 3, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("发动机")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("发动机故障", 4, 0, "正常", "报警"),
                    new WarningItem("再生循环", 4, 1, "正常", "报警"),
                    new WarningItem("机油压力低", 4, 2, "正常", "报警"),
                    new WarningItem("冷却液水温高", 4, 3, "正常", "报警"),
                    new WarningItem("吸油滤芯阻塞", 4, 5, "正常", "报警"),
                    new WarningItem("回油滤芯阻塞", 4, 6, "正常", "报警"),
                    new WarningItem("空气滤芯阻塞", 4, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("液位传感器")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("1号箱液位报警", 5, 0, "正常", "报警"),
                    new WarningItem("2号箱液位报警", 5, 1, "正常", "报警"),
                    new WarningItem("3号箱液位报警", 5, 2, "正常", "报警"),
                    new WarningItem("4号箱液位报警", 5, 3, "正常", "报警"),
                    new WarningItem("5号箱液位报警", 5, 4, "正常", "报警"),
                    new WarningItem("6号箱液位报警", 5, 5, "正常", "报警"),
                    new WarningItem("燃油液位低", 5, 6, "正常", "报警"),
                    new WarningItem("液压油液位低", 5, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("压力传感器")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("1号压力不足报警", 6, 0, "正常", "报警"),
                    new WarningItem("1号压力过高报警", 6, 1, "正常", "报警"),
                    new WarningItem("2号压力不足报警", 6, 2, "正常", "报警"),
                    new WarningItem("2号压力过高报警", 6, 3, "正常", "报警"),
                    new WarningItem("3号压力不足报警", 6, 4, "正常", "报警"),
                    new WarningItem("3号压力过高报警", 6, 5, "正常", "报警"),
                    new WarningItem("4号压力不足报警", 6, 6, "正常", "报警"),
                    new WarningItem("4号压力过高报警", 6, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("电源")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("电池电量不足", 7, 0, "正常", "报警"),
                    new WarningItem("电池电量严重不足", 7, 1, "正常", "报警"),
                    new WarningItem("系统电压过高", 7, 2, "正常", "报警"),
                    new WarningItem("控制电源丢失", 7, 3, "正常", "报警"),
                    new WarningItem("负载电源丢失", 7, 4, "正常", "报警"),
                    new WarningItem("电池通讯故障", 7, 6, "正常", "离线"),
                    new WarningItem("电池仓温度过高", 7, 7, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("通讯监控")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("通讯板通讯故障", 8, 0, "正常", "故障"),
                    new WarningItem("水炮通讯故障", 8, 1, "正常", "故障"),
                    new WarningItem("云台通讯故障", 8, 2, "正常", "故障"),
                    new WarningItem("雷达通讯故障", 8, 3, "正常", "故障"),
                    new WarningItem("超声波通讯故障", 8, 4, "正常", "故障"),
                    new WarningItem("工控机通讯故障", 8, 5, "正常", "故障"),
                    new WarningItem("气体传感器通讯故障", 8, 6, "正常", "故障"),
                }
            });
            Groups.Add(new WarningGroup("采样监控")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("可燃气体浓度过高", 9, 0, "正常", "报警"),
                    new WarningItem("有害气体浓度过高", 9, 1, "正常", "报警"),
                }
            });
            Groups.Add(new WarningGroup("采样监控")
            {
                Items = new List<WarningItem>()
                {
                    new WarningItem("导航系统故障", 10, 0, "正常", "故障"),
                }
            });
            
        }
        public WarningItem GetItem(int @byte,int bit)
        {
            WarningItem item = null;
            foreach(var group in Groups)
            {
                item=group.Items.Find(x=>x.Byte == @byte && x.Bit == bit);
                if (item != null) break;
            }
            return item;
        }
        public bool HasWarning()
        {
            return Groups.Exists(x => x.Warning);
        }
        public void SaveWarningLog()
        {
            if (HasWarning())
            {
                WarningTime = DateTime.Now;
            }
            foreach (var group in Groups)
            {
                foreach (var item in group.Items)
                {
                    item.Log();
                }
            }
        }
        public override void ProcessData(byte[] data, int start)
        {
            int i = start;
            switch (data[start])
            {
                case INFO_WARNING:
                    foreach (var group in Groups)
                    {
                        foreach (var item in group.Items)
                        {
                            item.Value = ((data[i + item.Byte] >> item.Bit) & 0x01) == 0x01;
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        public override void InitData()
        {
            
        }
    }
}
